﻿#define _CRT_SECURE_NO_WARNINGS 1;
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define A 10
// 20 静态创建链表
struct LinkNode {
	int data;
	struct LinkNode* next;
};

void test() {
	struct LinkNode node1 = { 10, NULL };
	struct LinkNode node2 = { 20, NULL };
	struct LinkNode node3 = { 30,NULL };
	struct LinkNode node4 = { 40,NULL };
	node1.next = &node2;
	node2.next = &node3;
	node3.next = &node4;
	//遍历链表
	//定义指针变量
	struct LinkNode* pcurrent = &node1;
	while (pcurrent != NULL) {
		printf("%d ", pcurrent->data);
		pcurrent = pcurrent->next;
	}
	printf("\n");
}
//21 动态创建链表
struct MoveNode {
	int data;
	struct MoveNode* next;
};
struct MoveNode* Init_MoveNode() {
	struct MoveNode* header = (struct MoveNode*)malloc(sizeof(struct MoveNode));
	header->data = 0;
	header->next = NULL;
	int val = 0;
	//尾部指针
	struct MoveNode* pRear = header;
	while (true) {
		scanf("%d", &val);
		if (val == -1) {
			break;
		}
		//创建新节点
		struct MoveNode* newnode = (struct MoveNode*)malloc(sizeof(struct MoveNode));
		newnode->data = val;
		newnode->next = NULL;
		pRear->next = newnode;
		pRear = newnode;
	}
	return header;

}
void qxy(struct MoveNode* header) {
	struct MoveNode* Pcurrent = header->next;
	while (Pcurrent != NULL) {
		printf("%d", Pcurrent->data);
		Pcurrent = Pcurrent->next;
		printf("\n");
	}

}


//22 学生成绩
typedef struct student ss;
struct student {
	char name[21];
	int num;
	int score[2];
	int sum;
};
//23 成绩普涨十昏
struct Student {
	char name[21];
	int num;
	int score;
};
//24 一个超级无敌完整的链表牢大
struct nnd {
	int date;
	struct nnd* Next;
};
//初始化链表
struct nnd* INIT_nnd() {
	//创建头节点
	struct nnd* header = (struct nnd*)malloc(sizeof(struct nnd));
	header->date;
	int Val = -1;
	header->Next = NULL;
	//尾部指针
	struct nnd* Prear = header;
	printf("输入数据:\n");
	while (true) {

		scanf("%d", &Val);
		if (Val == -1) {
			break;
		}
		//创建新节点
		struct nnd* NEWnode = (struct nnd*)malloc(sizeof(struct nnd));
		NEWnode->date = Val;
		NEWnode->Next = NULL;
		Prear->Next = NEWnode;
		Prear = NEWnode;
	}
	return header;

}
//遍历
void qxysb(struct nnd* header) {
	struct nnd* Pcurrent = header->Next;
	printf("当前的链表为:\n");
	while (Pcurrent != NULL) {
		printf("%d", Pcurrent->date);
		printf(" ");
		Pcurrent = Pcurrent->Next;
	}
	printf("\n");
}
//插入新节点
void Insert_INIT_nnd(struct nnd* header, int ov, int nv) {
	//两个辅助指针变量
	struct nnd* Pprev = header;
	struct nnd* Pcurrent = Pprev->Next;
	while (Pcurrent != NULL) {
		if (Pcurrent->date == ov) {
			break;
		}
		Pprev = Pcurrent;
		Pcurrent = Pcurrent->Next;
		printf("\n");
	}

	//如果PC为空，说明不存在
	if (Pcurrent == NULL) {
		return;
	}
	struct nnd* NEWnode = (struct nnd*)malloc(sizeof(struct nnd));
	NEWnode->date = nv;
	NEWnode->Next = NULL;
	//插入
	Pprev->Next = NEWnode;
	Pcurrent->Next = NEWnode;

}
//删除
void Del_nnd(struct nnd* header, int delv) {
	//需要两个辅助指针变量
	struct nnd* Pv = header;
	struct nnd* Pc = Pv->Next;
	while (Pc != NULL) {
		Pv = Pc;
		Pc = Pc->Next;
		if (Pc->date == delv) {
			break;
		}
	}
	Pv->Next = Pc->Next;//重新建立待删除节点与后继节点的关系
	free(Pc);//释放删除节点
	Pc = NULL;
}

//销毁
void Destroy_nnd(struct nnd* header) {
	struct nnd* PC = header->Next;
	while (PC != NULL) {
		//先保存下当前节点的下一节点地址
		struct nnd* PN = PC->Next;
		//释放当前节点内存哦
		free(PC);
		//指针后移
		PC = PN;

	}
}
//清空
void Clear_nnd(struct nnd* header) {
	struct nnd* PC = header->Next;
	while (PC != NULL) {
		struct nnd* PN = PC->Next;//保存下一个节点地址
		free(PC);
		PC = PN;

	}
	header->Next = NULL;

}






//初始化菜单 
int main() {
	int a = 0;
	while (a != 25) {
		printf("*****************************\n");
		printf("小小计算器\n");
		printf("1 加法\n");
		printf("2 减法\n");
		printf("3 乘法\n");
		printf("4 除法\n");
		printf("5 取模运算\n");
		printf("6 计算圆面积\n");
		printf("7 判断闰年\n");
		printf("8 成绩转换\n");
		printf("9 计算从1到输入数之间的奇数和\n");
		printf("10 计算从1到输入数之间的偶数和\n");
		printf("11 输出从1到输入之间所有3的倍数\n");
		printf("12 求即被3整除又被5整除的数\n");
		printf("13 求水仙花数\n");
		printf("14 求最大值\n");
		printf("15 求最大最小值\n");
		printf("16 十个数的冒泡排序\n");
		printf("17 一个3*4矩阵转置\n");
		printf("18 一个3*3的矩阵对角线和\n");
		printf("19 统计单词个数\n");
		printf("20 静态创建链表\n");
		printf("21 动态创建链表\n");
		printf("22 十个学生排序\n");
		printf("23 成绩普涨十分\n");
		printf("24 超级无敌完整链表\n");
		printf("25 退出\n");
		printf("请输入你要使用的部分\n");
		printf("****************************\n");
		scanf("%d", &a);
		if (a == 1) {
			printf("你要算的两个数：\n");
			int m;
			int n;
			int i;
			scanf("%d ", &m);
			scanf("%d", &n);
			i = m + n;
			printf("结果是:%d", i);
			printf("八进制结果是%o\n", i);
			printf("十六进制的结果是:%hx\n", i);

		}
		if (a == 2) {
			int m;
			int n;
			int i;
			printf("请输入你要算的两个数:");
			scanf("%d ", &m);
			scanf("%d", &n);
			i = m - n;
			printf("结果是:%d\n", i);
			printf("八进制结果是%o\n", i);
			printf("十六进制的结果是:%hx\n", i);

		}
		if (a == 3) {
			int m;
			int n;
			int i;
			printf("请输入你要算的两个数:");
			scanf("%d ", &m);
			scanf("%d", &n);
			i = m * n;
			printf("结果是:%d", i);
			printf("八进制结果是%o\n", i);
			printf("十六进制的结果是:%hx\n", i);

		}
		if (a == 4) {
			int m;
			int n;
			int i;
			int x;
			int coun = 0;
			printf("请输入你要算的两个数:");
			scanf("%d ", &m);
			scanf("%d", &n);
			if (n == 0) {
				printf("invalid value!\n");
			}
			else if (n != 0) {
				i = m / n;
				x = i;
				printf("结果是:%d\n", i);
				printf("八进制结果是%o\n", i);
				printf("十六进制的结果是:%hx\n", i);
				for (x; x > 0; x /= 10) {
					coun++;
				}
			}
			printf("这是%d位数", coun);
		}
		if (a == 5) {
			int m;
			int n;
			int i;
			printf("请输入你要算的两个数:");
			scanf("%d ", &m);
			scanf("%d", &n);
			i = m % n;
			printf("结果是:%d", i);
			printf("八进制结果是%o\n", i);
			printf("十六进制的结果是:%hx\n", i);
		}
		if (a == 6) {
			double PI = 3.14;
			printf("请输入圆的半径\n");
			double b;
			scanf("%lf", &b);
			double s = PI * b * b;
			printf("半径为%.2lf的圆的面积为%.2lf\n", b, s);
			break;
		}
		if (a == 7) {
			int year;
			printf("请输入年份：");
			scanf("%d", &year);
			if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
				printf("%d是闰年\n", year);
			else
				printf("%d是平年\n", year);
			system("pause");
		}
		if (a == 8) {
			float score;
			char grade;
			printf("请输入成绩:\n");
			scanf("%f", &score);
			while (score > 100 || score < 0) {
				printf("小伙计，你滴输入有错误");
				scanf("%f", &score);//重新输入 
			}
			switch ((int)score / 10) {
			case 10:printf("满昏！！");
				break;
			case 9:printf("差一点满魂");
				break;
			case 8: printf("良");
				break;
			case 7:printf("中");
				break;
			case 6:printf("鸡格");
				break;
			default:printf("不鸡格！！");
				break;
			}
		}
		if (a == 9) {
			int m;
			int n;
			int sum = 0;
			printf("请输入数字:");
			scanf("%d", &m);
			for (n = 1; n <= m; n += 2) {
				sum += n;
			}
			printf("结果是:%d", sum);
		}
		if (a == 10) {
			int m;
			int n = 0;
			int sum = 0;
			printf("请输入数字:");
			scanf("%d", &m);
			while (n <= m) {
				sum += n;
				n += 2;
			}
			printf("结果是:%d", sum);
		}
		if (a == 11) {
			printf("请输入数字:");
			int m = 1;
			int n;
			int i;
			scanf("%d", &n);
			do {
				m++;
				i = m % 3;
				if (i == 0) {
					printf("%d", m);
					printf(" ");
				}
			} while (m <= n);
		}
		if (a == 12) {
			int m;
			int n;
			printf("请输入数字:\n");
			scanf("%d", &m);
			scanf("%d", &n);
			for (m; m <= n; m++) {
				if (m % 3 == 0 && m % 5 == 0) {
					printf("%d", m);
				}
				else {
					printf("错误！母牛！");
					break;
				}
			}
		}
		if (a == 13) {
			int a, b, c, d;
			for (a = 100; a < 1000; a++) {
				b = a / 100;//百位 
				c = a % 100 / 10;//十位 
				d = a % 10;//个位 
				if (b * b * b + c * c * c + d * d * d) {
					printf("%d\t", a);
				}
			}
		}
		if (a == 14) {
			int a, b;
			printf("请输入数字:");
			scanf("%d%d", &a, &b);
			printf("%d和%d的最大值是%d", a, b, (a > b ? a : b));



		}
		if (a == 15) {
			int a, b;
			printf("请输入数字:");
			scanf("%d%d", &a, &b);
			printf("%d和%d的最大值是%d", a, b, (a < b ? a : b));
		}
		if (a == 16) {
			printf("请输入数据\n");
			int arr[10] = {};
			for (int b = 0; b < 10; b++) {
				scanf("%d", &arr[b]);
			}
			for (int i = 0; i < 10 - 1; i++)
			{
				for (int j = 0; j < 10 - 1 - i; j++)
				{
					if (arr[j] > arr[j + 1])
					{
						int tmp = arr[j];
						arr[j] = arr[j + 1];
						arr[j + 1] = tmp;
					}
				}
			}printf("排序后的为\n\n");
			for (int j = 0; j < 10; j++)
			{
				printf("%d\n", arr[j]);
			}
		}
		if (a == 17) {
			printf("请输入数据\n");
			int b[3][4];
			int row, line;

			//输入
			for (row = 0; row < 3; row++) {
				for (line = 0; line < 4; line++)
					scanf("%d", b[row][line]);
			}
			//显示输入的矩阵
			printf("你输入的矩阵是\n");
			for (row = 0; row < 3; row++) {
				for (line = 0; line < 4; line++)
					printf("%d\t", b[row][line]);
				printf("\n");
			}
			//转置
			int c[4][3];
			for (row = 0; row < 3; row++) {
				for (line = 0; line < 4; line++)//行列转换
					c[line][row] = b[row][line];//注意此处不能直接交换b中row和line 
			}
			printf("转置后的矩阵为\n");
			for (row = 0; row < 4; row++) {
				for (line = 0; line < 3; line++)
					printf("%d\t", c[row][line]);
				printf("\n");
			}
		}
		if (a == 18) {
			int arr[3][3] = { 0 };
			int sum = 0;
			for (int i = 0; i < 3; i++)
			{
				for (int j = 0; j < 3; j++)
				{
					scanf("%d", &arr[i][j]);
				}
			}
			for (int l = 0; l < 3; l++)
			{
				sum += arr[l][l];
			}  printf("%d", sum);
		}
		if (a == 19) {
			char c;
			printf("输入一行字符：\n");
			scanf("%s", &c);
			char ch;

			int i, count = 1, word = 0;
			while ((ch = getchar()) != '\n') {
				if (ch == ' ') {
					word = 0;
				}
				else if (word == 0) {
					word = 1;
					count++;
				}
			}
			printf("总共有 %d 个单词\n", count);
		}

		if (a == 20) {
			printf("静态链表的数值为:\n");
			test();
		}
		if (a == 21) {
			printf("请输入你的数据，烙铁:\n");
			struct MoveNode* header = Init_MoveNode();
			qxy(header);



		}
		if (a == 22) {
			ss* p = (ss*)malloc(sizeof(ss) * 10);
			for (int i = 0; i < 10; i++) {
				scanf("%s %d %d%d", p[i].name, &p[i].num, &p[i].score[0], &p[i].score[1]);
				p[i].sum = p[i].score[0] + p[i].score[1];
			}
			printf("原成绩为:\n");
			for (int i = 0; i < 10; i++) {
				printf("姓名:%s\n", p[i].name);
				printf("学号:%d\n", p[i].num);
				printf("语文:%d\n", p[i].score[0]);
				printf("数学%d\n", p[i].score[1]);
				printf("总成绩:%d\n", p[i].sum);
				printf("\n");
			}
			printf("排序后是:\n");
			for (int i = 0; i < 10 - 1; i++) {
				for (int j = 0; j < 10 - 1 - i; j++) {
					if (p[j].sum > p[j + 1].sum) {
						ss temp = p[j];
						p[j] = p[j + 1];
						p[j + 1] = temp;
					}
				}
			}
			for (int i = 0; i < 10; i++) {
				printf("姓名:%s\n", p[i].name);
				printf("学号:%d\n", p[i].num);
				printf("语文:%d\n", p[i].score[0]);
				printf("数学%d\n", p[i].score[1]);
				printf("总成绩:%d\n", p[i].sum);
				printf("\n\n");
			}
			free(p);

		}
		if (a == 23) {
			FILE* wok = fopen("D:\\my.txt", "r+");
			struct Student st[A];
			int i = 0;
			if (wok == nullptr) {
				printf("文件打开失败!\n");
				exit(0);
			}
			printf("文件内容是:\n");

			while (1)
			{
				if (feof(wok)) {
					break;
				}
				fscanf(wok, "%s\t%d\n", st[i].name, &st[i].score);
				printf("%s\t%d\n", st[i].name, st[i].score);
				i++;
			}
			rewind(wok);
			for (i = 0; i < A; i++) {
				fprintf(wok, "%s\t%d", st[i].name, st[i].score + 10);

			}
			fprintf(wok, "%s\t%d\n", st[A - 1].name, st[A - 1].score);
			fclose(wok);
			return EXIT_SUCCESS;

		}
		if (a == 24) {
			struct nnd* header = INIT_nnd();
			qxysb(header);
			Insert_INIT_nnd(header, 2, 8);
			qxysb(header);
			Del_nnd(header, 2);
			qxysb(header);
			printf("要继续么?是：1，否：2\n");
			int b = 0;
			scanf("%d", &b);
			if (b == 1) {
				Clear_nnd(header);
				Destroy_nnd(header);
			}
			else {
				break;
			}




		}
	}
}

